package practise.alogrithm.hundredInterviewQuestions.secondTime;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Richard on 7/26/2014.
 */
public class ContinuousSequence {
    public List<List<Integer>> find(List<Integer> array,Integer sum){
        List<List<Integer>> results = new ArrayList<>();
        for(int start=0,end=0,tempSum=array.get(start);array.get(start)<=sum/2;){

            if(tempSum==sum){
                List<Integer> result = new ArrayList<>();
                result.add(start);
                result.add(end);
                results.add(result);
                tempSum+=array.get(++end);
            }
            else if(tempSum<sum){
                tempSum+=array.get(++end);
            }
            else {
                tempSum-=array.get(start++);
            }
        }
        return results;
    }

    public static void main(String[] args){
        System.out.println(new ContinuousSequence().find(new ArrayList<Integer>(Arrays.asList(new Integer[]{1,2,3,4,5,6,7,8,9})),15));
    }
}
